from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtGui import QIcon

from my_utils.utils_class.Base_button import BaseButton


class CTButton(BaseButton):
    def __init__(self, ct_obj, parent=None):
        super().__init__(normal_icon_path="res/doctor_jiang.png",
                         hit_icon_path="res/dont_beat_me.png",
                         speed=ct_obj.speed,
                         parent=parent)
        self.ct = ct_obj

    def show_hit_effect(self):
        self.setIcon(QIcon(self.hit_icon_path))
        if self.ct.hp <= 0:
            QTimer.singleShot(0, self.cleanup)
        else:
            QTimer.singleShot(500, self.restore_icon)


    def restore_icon(self):
        self.setIcon(QIcon(self.normal_icon_path))
        self.move_randomly()

    @pyqtSlot()
    def hit(self):
        self.ct.reduce_hp(20)
        self.show_hit_effect()

